// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import '@/assets/css/base.css';
// import bridge from '@/assets/js/adapter';
import vConsoleDebug from '@/assets/js/vConsoleDebug';
import Axios from 'axios';
import toast from '@/components/Toast';
import layout from '@/components/layout';
import '@/assets/js/vant-reg';
Vue.component('layout', layout);
vConsoleDebug.initVConsole();
Vue.prototype.$toast = toast;
Vue.config.productionTip = false;
// Axios.defaults.baseURL = 'http://qdd.sztd123.cn';
Axios.defaults.headers = {};
// 添加一个请求拦截器
Axios.interceptors.request.use(function(config) {
  // console.log(config);
  // if (config.params) config.params.dspToken = localStorage.dspToken
  // config.headers.Token = localStorage.getItem('token');
  return config;
}, function(err) {
  return Promise.reject(err);
});

// 添加一个响应拦截器
Axios.interceptors.response.use(function(res) {
  // vConsoleDebug.logOriginData(res);
  let data = res.data;
  if (data.code && data.code.status === '1') {
    return data;
  }
  // toast(`发生错误，代码：${data.code.errorcode}`);
  toast('网络故障，请稍后再试');
  return Promise.reject(res);
}, function(err) {
  toast('网络故障，请稍后再试');
  return Promise.reject(err);
});
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  // 18000登录失效
  // if (Number(localStorage.dspTokenData) + 1800000 < Date.parse(new Date())) {
  //   localStorage.dspToken = '';
  // }
  // console.log(to);
  if (to.name !== 'login' && !localStorage.dspToken) {
    return next('login');
  }
  // console.log(to);
  next();
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
});
